<template>
  <div>
    <el-dialog :visible.sync="isShow" :title="{add: '新增权限', edit: '编辑权限'}[mode]" @close="closeEvent">
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="企业可见" prop="enVisible">
          <!-- <el-input v-model="name" /> -->
          <el-switch
            v-model="form.enVisible"
            active-text="可见"
            inactive-text="不可见"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="closeEvent">取消</el-button>
          <el-button @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 添加权限数据  编辑
import { sysPermissionPost, sysPermissionPut } from '@/api/permission.js'
export default {
  data() {
    return {
      mode: 'add',
      isShow: false,
      form: {
        enVisible: '0', //	string	非必须
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须   1代表添加页面; 2代表添加按钮
        pid: '' //	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        code: [{ required: true, message: '必填', trigger: 'blur' }],
        description: [{ required: true, message: '必填', trigger: 'blur' }],
        enVisible: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeEvent() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '0', //	string	非必须
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须   1代表添加页面; 2代表添加按钮
        pid: '' //	string	非必须
      }
    },
    submit() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          if (this.mode === 'add') {
            console.log(111)
            await sysPermissionPost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await sysPermissionPut(this.form)
            this.$message.success('编辑成功')
          }
          this.closeEvent()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style>

</style>
